﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AccountProxy = ChatForum.Client.AccountServiceProxy;
using ForumProxy = ChatForum.Client.ForumServiceProxy;

namespace ChatForum.Client.Admin
{
	public partial class Category : BasePage
    {
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
				LoadCategoryGrid();
		}

		private void LoadCategoryGrid()
		{
			HtmlGenericControl singleDiv = (HtmlGenericControl)PageHelper.FindControlRecursive(this, "firstCategoryDiv");
			IEnumerable<ForumProxy.Category> existingCategoryList = ServiceHelper.Instance.ForumService.GetAllCategory();
			if (existingCategoryList != null && existingCategoryList.Count() > 0)
			{
				//Hide the single div tag
				singleDiv.Style.Add("display", "none");
				Session["AllCategory"] = existingCategoryList;
				categoryListGrid.DataSource = Session["AllCategory"];
				categoryListGrid.DataBind();
			}
			else
			{
				//No category exist, show the single div tag
				singleDiv.Style.Add("display", "block");
			}
		}
		protected void OnDeleteCategory(object sender, EventArgs e)
		{

		}
		protected void UpdateCategory(object sender, EventArgs e)
		{
			string title = currentCategoryTitle.Value;
			string age = currentCategoryAge.Value;
			string desc = currentCategoryDescription.Value;

			if (Handler.ModifyCategory(title, age, desc))
				LoadCategoryGrid();
		}
		protected void OnAddCategory(object sender, EventArgs e)
		{
			string name = ((TextBox)categoryListGrid.FooterRow.FindControl("newName")).Text;
			string ageReq = ((TextBox)categoryListGrid.FooterRow.FindControl("newAgeRequirement")).Text;
			string desc = ((TextBox)categoryListGrid.FooterRow.FindControl("newDescription")).Text;
			if (Handler.AddCategory(name, ageReq, desc))
				LoadCategoryGrid();
		}
		protected void OnAddFirstCategory(object sender, EventArgs e)
		{
			if (Handler.AddCategory(txtName.Text, txtAge.Text, txtDescription.Text))
				LoadCategoryGrid();
		}
		protected void OnSearchCategory(object sender, EventArgs e)
		{

		}
	}
}